/******************************************************************************
	Copyright 2005-2007 R2@DevFx.NET
	DevFx.NET is free software; you can redistribute it and/or modify
	it under the terms of the Lesser GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	DevFx.NET is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	Lesser GNU General Public License for more details.

	You should have received a copy of the Lesser GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/*******************************************************************************/

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace HTB.DevFx.Security.Data
{
	internal class SqlNonSPDataProvider : BaseDataProvider
	{
		public SqlNonSPDataProvider() {
			this.providerFactory = SqlClientFactory.Instance;
		}

		private DbProviderFactory providerFactory;

		protected override DbProviderFactory ProviderFactory {
			get { return this.providerFactory; }
		}

		public override string ProviderName {
			get { return "SqlNonSPDataProvider(mssql2000 or above, non sp version)"; }
		}

		protected override void ExecuteCommand(ProviderDataType dataType, StatementType statement, DbCommand command, DataRow row) {
			if (dataType == ProviderDataType.Relation || statement == StatementType.Insert) {
				command.CommandText += ";SELECT @@IDENTITY";
				object scalarObject = command.ExecuteScalar();
				if(!Convert.IsDBNull(scalarObject)) {
					row["ID"] = Convert.ToInt32(scalarObject);
				}
			} else {
				base.ExecuteCommand(dataType, statement, command, row);
			}
		}
	}
}